<?php
/**
 * @name /lib/private/Trip.php
 * @author Mohammad Shabbir Hasan <shabbir_cse03@yahoo.com>
 * @version 2011.03.25
 *
 * A trip class to manage trips.
 */

/**
 * Trip class.
 *
 */

ReqLib('/lib/private/User.php');
ReqLib('/lib/private/Orphanage.php');

class Trip {
  /**
   * Trip identification.
   *
   * @var integer
   */
  var $TripId;
  /**
   * Start Date of a trip.
   *
   * @var string
   */
  var $TripStartDate;
  /**
   * Notes about a trip.
   *
   * @var string
   */
  var $Notes;

  /**
  *GET SCUD
  * GetTripIdsByUserID()
  *
  */

  /**
  *GET SCUD
  * GetTripIdsByOrphanageID()
  *
  */

  /**
  *
  * Constructor.
  *
  */
  function Trip(){
  	$this->TripId = 0;
	$this->UserId = 0;
	$this->OrphanageId = 0;
	$this->TripStartDate = '';
	$this->Notes = '';
  }

   /**
   * Gets a subset of orphanages, sorted by orphanage id,
   * with an optional offset and maximum amount.
   *
   * @param integer
   * @param integer
   * @return Trips[]
   */
  function GetTrips($offset = 0, $maxResults = 0) {
    $objects = array();
    $db = new DB();
    // Initial query
    $query = "SELECT t.TripId FROM trips t ORDER BY t.TripId";
    // Look for max results and offsets
    if ($maxResults && $maxResults * 1 == $maxResults) {
      $query .= " LIMIT $maxResults";
      if ($offset && $offset * 1 == $offset)
        $query .= " OFFSET $offset";
    }
    else if ($offset && $offset * 1 == $offset)
      $query .= " LIMIT 99999999 OFFSET $offset";
    // Get the ids
    $ids = $db->QueryCol($query) or die();

    // If we have ids, load the objects
    if (is_array($ids))
      foreach ($ids as $id) {
        $objects[count($objects)] = new Trip();
        $objects[count($objects) - 1]->Load($id);
      }

	return $objects;
  }

    /**
   * Determines if a trip exists or not.
   *
   * @param integer
   * @return boolean
   */
  function Exists($tripId){
    $db = new DB();
    $sql = "SELECT t.TripId FROM trips t WHERE t.TripId='" . addslashes($tripId) . "' LIMIT 1;";
    return ($db->QueryVal($sql) > 0);
  }

/**
   * Returns a string representation of the object.
   *
   * @param string
   * @return string
   */
  function ToString($delimiter = "<br />") {
    $s = "";
    $s .= "TripId: " . $this->TripId . $delimiter;
    $s .= "UserId: " . $this->UserId . $delimiter;
    $s .= "OrphanageId: " . $this->OrphanageId . $delimiter;
    $s .= "TripStartDate: " . $this->TripStartDate . $delimiter;
    $s .= "Notes: " . $this->Notes . $delimiter;
    return $s;
  }

  /**
   * Loads a Trip; returns true on success, false on failure.
   *
   * @param integer
   * @return boolean
   */
  function Load($tripId) {
    $db = new DB();
    // Prevent SQL injection once to ease coding later
    $tripId = addslashes($tripId);
    // Select bulk of the data
    $query = <<<HEREDOC
      SELECT
        t.TripId,
        t.UserId AS UserId,
        t.OrphanageId AS OrphanageId,
        t.TripStartDate AS TripStartDate,
        t.Notes As Notes
      FROM
        trips t
      INNER JOIN
        users u
      ON
        u.UserId=t.UserId
      INNER JOIN
        orphanages o
      ON
        o.OrphanageId=t.OrphanageId
      WHERE
        t.TripId='{$tripId}'
HEREDOC;
    $data = $db->QueryRow($query);
    // If we can't find it, we can't load it
    if (!$data)
      return false;
    // Assign response
    $this->TripId = $data['TripId'];
    $this->UserId = $data['UserId'];
    $this->OrphanageId = $data['OrphanageId'];
    $this->TripStartDate = $data['TripStartDate'];
    $this->Notes = $data['Notes'];

    // If we made it this far, we've loaded the object
    return true;
  }

  /**
   * Creates a trip if unique, and returns the trip id;
   * returns true on success, false on failure.
   *
   * @param string
   * @return boolean
   */
  function Create($userId, $orphanageId) {
    if (!User::Exists($userId))
      return false;

	if (!Orphanage::Exists($orphanageId))
      return false;

    $db = new DB();

    // Create a new trip record

    $sql = "INSERT INTO trips (UserId, OrphanageId) VALUES ('$userId', '$orphanageId');";

    $db->Query($sql) or die();
    // Assign the newly-created id
    $sql = "SELECT LAST_INSERT_ID();";
    $this->TripId = $db->QueryVal($sql) or die();

	  return true;
  }

  /**
   * Saves a PRE-EXISTING Trip; returns true on success, false on failure.
   *
   * @return boolean
   */
  function Save() {
    /*if (!User::Exists($userId))
      return false;

	if (!Orphanage::Exists($orphanageId))
      return false;*/

      $db = new DB();

    // Save the core orphanage data
    $sql =
      "UPDATE trips SET " .
        "UserId='" . addslashes($this->UserId) . "', " .
		"OrphanageId='" . addslashes($this->OrphanageId) . "', " .
		"TripStartDate='" . addslashes($this->TripStartDate) . "', " .
		"Notes='" . addslashes($this->Notes) . "' " .
      "WHERE " .
        "TripId='" . addslashes($this->TripId) . "';";

	$db->Query($sql) or die();

    return true;
  }

  /**
   * Deletes a trip; returns true on success, false on failure.
   *
   * @static
   * @param integer
   * @return boolean
   */
  function Delete($tripId) {
    if (!Trip::Exists($tripId))
      return false;
    $db = new DB();

    $db->Query("DELETE FROM trips WHERE TripId='" . addslashes($tripId) . "';");
    return true;
  }

  /**
   * Gets the ids of the trips based on user id;
   * uses a whitespace- and case-insensitive search.
   *
   * @static
   * @param string
   * @return int array
   */
  function GetTripIdsByUserId($UserId){
	$db = new DB();

	$tripIds = array();

	$tripIds = $db->QueryCol("SELECT TripId FROM trips Where UserId='" . addslashes($UserId) . "';");

	return $tripIds;
  }

  /**
   * Gets the ids of a trip based on orphanage id;
   * uses a whitespace- and case-insensitive search.
   *
   * @static
   * @param string
   * @return int array
   */
  function GetTripIdsByOrphanageId($OrphanageId){
	$db = new DB();

	$tripIds = array();

	$tripIds = $db->QueryCol("SELECT TripId FROM trips Where OrphanageId='" . addslashes($OrphanageId) . "';");

	return $tripIds;
  }

}
